package controller

import (
	"back/app/config"
	"back/app/model"
	"github.com/gin-gonic/gin"
	"io/ioutil"
	"net/http"
	"strconv"
)

type Admin struct {
	warp
}

func (s *Admin) AddUserHandler(c *gin.Context) {
	cont, _ := ioutil.ReadAll(c.Request.Body)

	userType, _ := strconv.Atoi(c.GetString("type"))
	if userType < 1 {
		config.GetLogger().Warnw("权限不足")
		c.JSON(http.StatusOK, s.FailWarp("权限不足"))
		return
	}

	user := model.NewAdmin()

	err, data := user.GetAddUserData(string(cont))
	if err != nil {
		config.GetLogger().Warnw("添加用户失败",
			"err", err.Error(),
		)
		c.JSON(http.StatusOK, s.FailWarp(err.Error()))
		return
	}

	c.JSON(http.StatusOK, s.SuccessWarp(data))
}

func (s *Admin) RemoveUserHandler(c *gin.Context) {
	cont, _ := ioutil.ReadAll(c.Request.Body)

	userType, _ := strconv.Atoi(c.GetString("type"))
	if userType < 1 {
		config.GetLogger().Warnw("权限不足")
		c.JSON(http.StatusOK, s.FailWarp("权限不足"))
		return
	}

	user := model.NewAdmin()

	err, data := user.GetRemoveUserData(string(cont))
	if err != nil {
		config.GetLogger().Warnw("移除用户失败",
			"err", err.Error(),
		)
		c.JSON(http.StatusOK, s.FailWarp(err.Error()))
		return
	}

	c.JSON(http.StatusOK, s.SuccessWarp(data))
}

func (s *Admin) DelUserHandler(c *gin.Context) {
	cont, _ := ioutil.ReadAll(c.Request.Body)

	userType, _ := strconv.Atoi(c.GetString("type"))
	if userType < 2 {
		config.GetLogger().Warnw("权限不足")
		c.JSON(http.StatusOK, s.FailWarp("权限不足"))
		return
	}

	user := model.NewAdmin()

	err, data := user.GetDeleteUserData(string(cont))
	if err != nil {
		config.GetLogger().Warnw("删除用户失败",
			"err", err.Error(),
		)
		c.JSON(http.StatusOK, s.FailWarp(err.Error()))
		return
	}

	c.JSON(http.StatusOK, s.SuccessWarp(data))

}

func (s *Admin) CheckFeedbackHandler(c *gin.Context) {
	cont, _ := ioutil.ReadAll(c.Request.Body)

	userType, _ := strconv.Atoi(c.GetString("type"))
	if userType < 2 {
		config.GetLogger().Warnw("权限不足")
		c.JSON(http.StatusOK, s.FailWarp("权限不足"))
		return
	}

	user := model.NewAdmin()

	err, data := user.GetCheckFeedbackData(string(cont))
	if err != nil {
		config.GetLogger().Warnw("查询用户反馈信息失败",
			"err", err.Error(),
		)
		c.JSON(http.StatusOK, s.FailWarp(err.Error()))
		return
	}

	c.JSON(http.StatusOK, s.SuccessWarp(data))
}

func (s *Admin) CheckUserListHandler(c *gin.Context) {
	cont, _ := ioutil.ReadAll(c.Request.Body)

	userType, _ := strconv.Atoi(c.GetString("type"))
	if userType < 1 {
		config.GetLogger().Warnw("权限不足")
		c.JSON(http.StatusOK, s.FailWarp("权限不足"))
		return
	}

	user := model.NewAdmin()

	err, data := user.GetCheckUserListData(string(cont))
	if err != nil {
		config.GetLogger().Warnw("查看用户列表失败",
			"err", err.Error(),
		)
		c.JSON(http.StatusOK, s.FailWarp(err.Error()))
		return
	}

	c.JSON(http.StatusOK, s.SuccessWarp(data))

}

func (s *Admin) RemoveManagerHandler(c *gin.Context) {
	cont, _ := ioutil.ReadAll(c.Request.Body)

	userType, _ := strconv.Atoi(c.GetString("type"))
	if userType < 2 {
		config.GetLogger().Warnw("权限不足")
		c.JSON(http.StatusOK, s.FailWarp("权限不足"))
		return
	}

	user := model.NewAdmin()

	err, data := user.GetRemoveManagerData(string(cont))
	if err != nil {
		config.GetLogger().Warnw("移除管理员失败",
			"err", err.Error(),
		)
		c.JSON(http.StatusOK, s.FailWarp(err.Error()))
		return
	}

	c.JSON(http.StatusOK, s.SuccessWarp(data))

}

func (s *Admin) AddManagerHandler(c *gin.Context) {
	cont, _ := ioutil.ReadAll(c.Request.Body)

	userType, _ := strconv.Atoi(c.GetString("type"))
	if userType < 2 {
		config.GetLogger().Warnw("权限不足")
		c.JSON(http.StatusOK, s.FailWarp("权限不足"))
		return
	}

	user := model.NewAdmin()

	err, data := user.GetAddManagerData(string(cont))
	if err != nil {
		config.GetLogger().Warnw("添加管理员失败",
			"err", err.Error(),
		)
		c.JSON(http.StatusOK, s.FailWarp(err.Error()))
		return
	}

	c.JSON(http.StatusOK, s.SuccessWarp(data))

}

func (s *Admin) ReplyFbHandler(c *gin.Context) {
	cont, err := ioutil.ReadAll(c.Request.Body)

	userType, _ := strconv.Atoi(c.GetString("type"))
	if userType < 2 {
		config.GetLogger().Warnw("权限不足")
		c.JSON(http.StatusOK, s.FailWarp("权限不足"))
		return
	}

	if cont == nil {
		config.GetLogger().Warnw("反馈回复不能为空",
			"cont:", cont,
		)
		c.JSON(http.StatusOK, s.FailWarp("反馈回复不能为空"))
		return
	}

	user := model.NewAdmin()

	err, data := user.GetReplyFbData(string(cont))
	if err != nil {
		config.GetLogger().Warnw("回复反馈失败",
			"err", err.Error(),
		)
		c.JSON(http.StatusOK, s.FailWarp(err.Error()))
		return
	}

	c.JSON(http.StatusOK, s.SuccessWarp(data))
}

func (s *Admin) CheckApplyListHandler(c *gin.Context) {
	cont, _ := ioutil.ReadAll(c.Request.Body)

	userType, _ := strconv.Atoi(c.GetString("type"))
	if userType < 2 {
		config.GetLogger().Warnw("权限不足")
		c.JSON(http.StatusOK, s.FailWarp("权限不足"))
		return
	}

	user := model.NewAdmin()

	err, data := user.GetCheckApplyListData(string(cont))
	if err != nil {
		config.GetLogger().Warnw("查看申请列表失败",
			"err", err.Error(),
		)
		c.JSON(http.StatusOK, s.FailWarp(err.Error()))
		return
	}

	c.JSON(http.StatusOK, s.SuccessWarp(data))
}

func (s *Admin) CheckManagerListHandler(c *gin.Context) {
	cont, _ := ioutil.ReadAll(c.Request.Body)

	userType, _ := strconv.Atoi(c.GetString("type"))
	if userType < 2 {
		config.GetLogger().Warnw("权限不足")
		c.JSON(http.StatusOK, s.FailWarp("权限不足"))
		return
	}

	user := model.NewAdmin()

	err, data := user.GetCheckManagerListData(string(cont))
	if err != nil {
		config.GetLogger().Warnw("查看管理员列表失败",
			"err", err.Error(),
		)
		c.JSON(http.StatusOK, s.FailWarp(err.Error()))
		return
	}

	c.JSON(http.StatusOK, s.SuccessWarp(data))
}

func (s *Admin) CheckAllUserListHandler(c *gin.Context) {
	cont, _ := ioutil.ReadAll(c.Request.Body)

	userType, _ := strconv.Atoi(c.GetString("type"))
	if userType < 2 {
		config.GetLogger().Warnw("权限不足")
		c.JSON(http.StatusOK, s.FailWarp("权限不足"))
		return
	}

	user := model.NewAdmin()

	err, data := user.GetCheckAllUserListData(string(cont))
	if err != nil {
		config.GetLogger().Warnw("查看管理员列表失败",
			"err", err.Error(),
		)
		c.JSON(http.StatusOK, s.FailWarp(err.Error()))
		return
	}

	c.JSON(http.StatusOK, s.SuccessWarp(data))
}

func (s *Admin) CheckLoginLoggerListHandler(c *gin.Context) {
	cont, _ := ioutil.ReadAll(c.Request.Body)

	userType, _ := strconv.Atoi(c.GetString("type"))
	if userType < 2 {
		config.GetLogger().Warnw("权限不足")
		c.JSON(http.StatusOK, s.FailWarp("权限不足"))
		return
	}

	user := model.NewAdmin()

	err, data := user.GetCheckLoginLoggerListData(string(cont))
	if err != nil {
		config.GetLogger().Warnw("查看管理员列表失败",
			"err", err.Error(),
		)
		c.JSON(http.StatusOK, s.FailWarp(err.Error()))
		return
	}

	c.JSON(http.StatusOK, s.SuccessWarp(data))
}